package org.simple.framework.core.enums;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 项目名称：simple-framework
 * 类名称：ValueEnum
 * 类描述：ValueEnum
 * 创建时间：2018/8/15
 *
 * @author sisyphus   (E-mail:1620657419@qq.com)
 * @version v1.0
 */
public interface ValueEnum {

    /**
     * 获得名称
     *
     * @return .
     */
    String getName();

    /**
     * 获得值
     *
     * @return .
     */
    String getCode();

    static <T extends Enum<T> & ValueEnum> T getByCode(Class<T> clazz, String code) {
        T[] values = clazz.getEnumConstants();
        if (code != null && values != null) {
            for (T value : values) {
                if (Objects.equals(value.getCode(), code)) {
                    return value;
                }
            }
        }
        return null;
    }

    static <T extends Enum<T> & ValueEnum> void validateCode(Class<T> clazz, String code, String notfoundMsg) {
        T byCode = getByCode(clazz, code);

        if (byCode == null) {
            throw new NullPointerException(notfoundMsg);
        }
    }

    static <T extends Enum<T> & ValueEnum> List<String> getAllCode(Class<T> clazz) {
        T[] values = clazz.getEnumConstants();
        return Arrays.stream(values).map(T::getCode).collect(Collectors.toList());
    }

}
